<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
        #whirlyThing{display: none;}

    </style>
    <title>创建自定义事件</title>
</head>
<body>
    <button type="button" id="clickMe">Start</button>
    <img src="831.gif" alt="" id="whirlyThing">

    <script>
        function triggerEvent(target, eventType, eventDetail) {
            const event = new CustomEvent(eventType, {
                detail: eventDetail
            });
            target.dispatchEvent(event);
        }

        function performAjaxOperation() {
            triggerEvent(document, 'ajax-start', {url: 'my-url'});
            setTimeout(() => {
                triggerEvent(document, 'ajax-complete');
            }, 5000);
        }
        const button = document.getElementById("clickMe");
        button.addEventListener("click", () => {
            performAjaxOperation();
        });
        document.addEventListener('ajax-start', e=>{
            document.getElementById('whirlyThing').style.display = 'inline-block';
            console.log(e.detail.url)
        });
        document.addEventListener('ajax-complete', e=>{
            document.getElementById('whirlyThing').style.display = 'none';
        })
    </script>
</body>
</html>